<template>
  <d2-container>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-row>
        <el-col :span="7">
          <el-form-item label="账号">
            <el-input v-model="dataForm.username" placeholder="请输入账号名称"  clearable style="100%"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="设备编号">
            <el-input v-model="dataForm.meterCode" placeholder="请输入设备编号"  clearable style="100%"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="日期" label-width="40px">
            <el-date-picker
              v-model="dataForm.date"
              placeholder="时间范围"
              style="width: 350px;"
              class="filter-item"
              size="medium"
              :picker-options="pickerOptions"
              type="datetimerange"
              suffix-icon="el-icon-date"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item>
            <el-button type="primary"  size="small" @click="getDataList()">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <!--<el-table-column-->
        <!--type="selection"-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--width="50">-->
      <!--</el-table-column>-->
      <el-table-column
        prop="logId"
        header-align="center"
        align="center"
        label="日志id">
      </el-table-column>
      <el-table-column
        prop="logType"
        header-align="center"
        align="center"
        label="操作类型">
        <template slot-scope="scope">
          {{scope.row.logType|stateName(stateList)}}
        </template>
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        label="账号">
        <template slot-scope="scope">
          {{scope.row.userId|userName(uselist)}}
        </template>
      </el-table-column>
      <el-table-column
        prop="meterCode"
        header-align="center"
        align="center"
        label="设备编号">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="操作时间">
      </el-table-column>
      <!--<el-table-column-->
        <!--header-align="center"-->
        <!--align="center"-->
        <!--width="150"-->
        <!--label="操作">-->
        <!--<template slot-scope="scope">-->
          <!--<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.logId)">修改</el-button>-->
          <!--<el-button type="text" size="small" @click="deleteHandle(scope.row.logId)">删除</el-button>-->
        <!--</template>-->
      <!--</el-table-column>-->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
  </d2-container>
</template>

<script>
  import AddOrUpdate from './ecslog-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          username: '',
          meterCode:'',
          date:''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        stateList: [{"name":"登录","value":"01"},{"name":"拉闸","value":"02"},{"name":"合闸","value":"03"}],
        addOrUpdateVisible: false,
        uselist: [],
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getUserDataList()
      this.getDataList()
    },
    filters: {
      stateName: function (value, statusList) {
        for (let i = 0; i < statusList.length; i++) {
          if (statusList[i].value === value) {
            return statusList[i].name;
          }
        }
        return value;
      },
      userName: function (value, statusList) {
        for (let i = 0; i < statusList.length; i++) {
          if (statusList[i].userId === value) {
            return statusList[i].username;
          }
        }
        return value;
      }
    },
    methods: {

      // 获取数据列表
      getUserDataList () {
        this.dataListLoading = true
        this.$api.okhttp({
          url: this.$http.adornUrl('/sys/user/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 10000,
            "customerId": this.$util.cookies.get('customer_id'),
          })
        }).then((data) => {
          if (data && data.code === 0) {
          this.uselist = data.page.list
        } else {
          this.uselist = []
        }
      })
      },

      // 获取数据列表
      getDataList (type) {
        if(type){
          this.pageIndex = 1
        }

        let startDate = ''
        let endDate = ''
        if(this.dataForm.date!==''){
          startDate = this.dataForm.date[0].format('yyyy-MM-dd hh:mm:ss')
          endDate = this.dataForm.date[1].format('yyyy-MM-dd hh:mm:ss')
        }
        this.dataListLoading = true
        this.$api.okhttp({
          url: this.$http.adornUrl('/ecs/ecslog/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'username': this.dataForm.username,
            'meterCode': this.dataForm.meterCode,
            'startDate': startDate,
            'endDate': endDate
          })
        }).then((data) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.logId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.okhttp({
            url: this.$http.adornUrl('/ecs/ecslog/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
